package com.six.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.six.pojo.AgreementDetail;
import com.six.result.Result;
import com.six.result.ResultUtils;
import com.six.service.AgreementDetailService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "采购合同明细列表接口文档")
@RestController
@RequestMapping("/agreementDetail")
@Slf4j
@CrossOrigin
public class AgreementDetailController {

    @Autowired
    private AgreementDetailService detailService;

    @PostMapping("/list")
    public Result findList(){
        return null;
    }

    @PostMapping("/add")
    public Result add(@RequestBody AgreementDetail detail){

        detail.setId(null);
        detailService.save(detail);
        return ResultUtils.buildSuccess();
    }

    @PutMapping("/update")
    public Result update(@RequestBody AgreementDetail detail){

        detailService.updateById(detail);
        return ResultUtils.buildSuccess();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        detailService.removeById(id);
        return ResultUtils.buildSuccess();
    }


    /**
     * 根据合同id查询采购合同明细列表
     */
    @GetMapping("/list/{id}")
    public Result findListByAgreementId(@PathVariable Long id){

        LambdaQueryWrapper<AgreementDetail> queryWrapper = new LambdaQueryWrapper<>();
        //据合同id查询采购合同明细列表
        queryWrapper.eq(AgreementDetail::getAgreementId,id);
        List<AgreementDetail> list = detailService.list(queryWrapper);
        //返回列表
        return ResultUtils.buildSuccess((long) list.size(),list);
    }

}
